#include "Shm.hpp"

int main()
{
    key_t Key = ftok(pathname.c_str(), proj_id);
    if (Key < 0)
    {
        perror("ftok");
        exit(1);
    }

    int shmid = shmget(Key, ShmSize, IPC_CREAT | IPC_EXCL | 0666); // 创建共享内存
    if (shmid < 0)
    {
        perror("shmget error!");
        exit(1);
    }

    std::cout << "Key: " << ToHex(Key) << "\n"
              << "Shmid: " << shmid << std::endl;

    // 关联挂接
    char *shmaddr = (char *)shmat(shmid, NULL, 0 ); 
    std::cout << "server shmat success" << std::endl;

    while(true)
    {
        sleep(2);
        std::cout << shmaddr << std::endl;
    }

    // 去关联
    shmdt(shmaddr);
    std::cout << "server shmdt success" << std::endl;

    shmctl(shmid, IPC_RMID, NULL); // 删除共享内存

    return 0;
}